package cn.itrip.trade.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.common.DtoUtil;
import cn.itrip.trade.config.WXPayConfigImpl;
import cn.itrip.trade.service.OrderService;
import cn.itrip.trade.wx.WXPay;
import cn.itrip.trade.wx.WXPayUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/wxpay")
public class WxPaymentController {

    private Logger logger = Logger.getLogger(WxPaymentController.class);

    @Resource
    private OrderService orderService;

    @Resource
    private WXPayConfigImpl wxPayConfigImpl;

    /***
     * 生成微信支付二维码
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "订单微信支付", httpMethod = "GET",
            protocols = "HTTP", produces = "application/xml", consumes = "application/x-www-form-urlencoded",
            response = String.class,
            notes = "客户端提交订单支付请求，对该API的返回结果不用处理，浏览器将自动跳转至微信支付二维码页面。<br><b>请使用普通表单提交，不能使用ajax异步提交。</b>")
    @RequestMapping(value = "/createqccode/{orderNo}",method = RequestMethod.GET)
    @ResponseBody
    public Dto createCode(@PathVariable String orderNo) {
        try {
            ItripHotelOrder order = orderService.loadItripHotelOrder(orderNo);
            if (order == null || order.getOrderStatus() != 0) {
                return DtoUtil.returnFail("订单状态异常", "110001");
            }
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", "爱旅行项目订单支付");
            data.put("out_trade_no", orderNo);
            data.put("device_info", "");
            data.put("fee_type", "CNY");
            data.put("total_fee", "1");
            data.put("spbill_create_ip", "123.12.12.123");
            data.put("notify_url", wxPayConfigImpl.getNotifyUrl());
            data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
            data.put("product_id", "1");

            WXPay wxpay = new WXPay(wxPayConfigImpl);
            Map<String, String> resp = wxpay.unifiedOrder(data);
            logger.info(resp);
            if (resp.get("return_code").equals("SUCCESS") && resp.get("result_code").equals("SUCCESS")){
                Map<String,String> result = new HashMap<>();
                result.put("codeUrl",resp.get("code_url"));
                result.put("hotelName", order.getHotelName());
                result.put("roomId", order.getRoomId().toString());
                result.put("count", order.getCount().toString());
                result.put("payAmount", order.getPayAmount().toString());
                return DtoUtil.returnDataSuccess(result);
            } else {
                logger.info(resp.get("return_msg"));
                return DtoUtil.returnFail("订单支付异常", "110002");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("订单运行异常", "110003");
        }

    }


    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> paymentCallBack(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> result = new HashMap<String, String>();
        Map<String, String> params = null;
        try {
            WXPay wxPay = new WXPay(wxPayConfigImpl);
            //1.从request获取XML流，转化成为MAP数据
            InputStream inputStream = request.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuffer sb = new StringBuffer();
            String temp;
            while ((temp = reader.readLine()) != null) {
                sb.append(temp);
            }
            reader.close();
            inputStream.close();
            params = WXPayUtil.xmlToMap(sb.toString());
            logger.info(params);
            boolean flag = wxPay.isResponseSignatureValid(params);
            logger.info("2.notify-flag:" + flag);

            if (flag){
                String returnCode = params.get("return_code");
                logger.info("3.returnCode:" + returnCode);

                if (returnCode.equals("SUCCESS")) {
                    String transactionId = params.get("transaction_id");
                    String outTradeNo = params.get("out_trade_no");
                    if (!orderService.processed(outTradeNo)) {
                        orderService.paySuccess(outTradeNo, 2, transactionId);
                    }
                    logger.info("4.订单：" + outTradeNo + " 交易完成" + ">>>" + transactionId);
                } else {
                    result.put("return_code", "FAIL");
                    result.put("return_msg", "支付失败");
                    logger.info("支付失败");
                }
            } else {
                result.put("return_code", "FAIL");
                result.put("return_msg", "签名失败");
                logger.info("签名验证失败>>>>>>>>>>>>");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @ApiOperation(value = "订单微信支付刷单程序", httpMethod = "GET",
            protocols = "HTTP", produces = "application/xml", consumes = "application/x-www-form-urlencoded",
            response = String.class,
            notes = "前端用户检测订单是否成功的接口<br>如果订单状态为2则代表订单支付成功<b></b>")
    @RequestMapping(value = "/queryorderstatus/{orderNo}", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripHotelOrder> queryOrderIsSuccess(@PathVariable String orderNo) {
        ItripHotelOrder order = null;
        try {
            order = orderService.loadItripHotelOrder(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(order);
    }
}
